package com.net.oil.main.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.net.oil.R;
import com.net.oil.base.BaseDialogFragment;
import com.net.oil.main.adapter.CouponRecyclerViewAdapter;
import com.net.oil.main.bean.NewAccountCoupon;
import com.net.oil.main.contract.NewAccountCouponContract;
import com.net.oil.main.presenter.NewAccountCouponPresenter;
import com.net.oil.widget.ToastUtil;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class NewAccountCouponDialogFragment extends BaseDialogFragment<NewAccountCouponPresenter> implements NewAccountCouponContract.View {

    private static final String CouponDataKey = "couponData";

    @BindView(R.id.coupon_recycler_view)
    RecyclerView couponRecyclerView;
    Unbinder unbinder;
    private CouponRecyclerViewAdapter adapter;

    public static NewAccountCouponDialogFragment newInstance(List<NewAccountCoupon> newAccountCoupons) {

        Gson gson = new Gson();
        String value = gson.toJson(newAccountCoupons);

        Bundle args = new Bundle();
        args.putString(CouponDataKey, value);
        NewAccountCouponDialogFragment fragment = new NewAccountCouponDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, R.style.ThemeLightDialogFragment);
        presenter = new NewAccountCouponPresenter(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_new_account_coupon_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    private void initData() {
        Bundle bundle = getArguments();
        String value = bundle.getString(CouponDataKey);
        Gson gson = new Gson();
        List<NewAccountCoupon> datas = gson.fromJson(value, new TypeToken<List<NewAccountCoupon>>() {
        }.getType());
        adapter.updateDatas(datas);
    }

    private void initView() {
        adapter = new CouponRecyclerViewAdapter(getContext());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        couponRecyclerView.setLayoutManager(linearLayoutManager);
        couponRecyclerView.setAdapter(adapter);
        adapter.setOnGetCoupon(new CouponRecyclerViewAdapter.OnGetCoupon() {
            @Override
            public void getCoupon(int id) {
                /* 领取优惠券按钮点击回调 */
                presenter.getNewAccountCoupon(id);
                showProgressDialog();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 领取优惠券成功
     */
    @Override
    public void getNewAccountCouponSuccess() {
        dismissProgressDialog();
        ToastUtil.showSuccessToast(getContext(), "领取成功");
        dismiss();
    }

    /**
     * 领取优惠券失败
     */
    @Override
    public void getNewAccountCouponFailed() {
        dismissProgressDialog();
        ToastUtil.showErrorToast(getContext(), "领取失败");
    }
}
